<%--
  Created by IntelliJ IDEA.
  User: 讨厌你的一切
  Date: 2020/7/18
  Time: 18:37
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
<jsp:include page="/common/common-js.jsp"></jsp:include>
<!-- 数据表格 -->
<div>
    <div style="margin-left:20px">
        <button class="btn btn-primary btn-sm" onclick="showAddPermissionDialog()">
            <span class="glyphicon glyphicon-plus"></span> 新增
        </button>
        <button class="btn btn-primary btn-sm" onclick="showUpdatePermissionDialog()">
            <span class="glyphicon glyphicon-pencil"></span> 修改
        </button>
        <button class="btn btn-danger btn-sm" onclick="showDeletePermissionDialog()">
            <span class="glyphicon glyphicon-pencil"></span> 删除
        </button>
    </div>
</div>
<div>
    <ul class="ztree" id="ztree"></ul>
</div>
<!-- 新增权限DIV -->
<div id="addPermissionDiv" style="display:none">
    <!-- 新增权限form -->
    <form id="addPermissionForm" class="form-horizontal">

        <div class="form-group">
            <label class="col-sm-2 control-label">权限名称:</label>
            <div class="col-sm-8">
                <input type="text" class="form-control" name="name" />
            </div>
        </div>


    </form>
</div>

<div id="updatePermissionDiv" style="display:none">
    <!-- 新增权限form -->
    <form id="updatePermissionForm" class="form-horizontal">
        <input type="text" id="areaId">
        <div class="form-group">
            <label class="col-sm-2 control-label">权限名称:</label>
            <div class="col-sm-8">
                <input type="text" class="form-control" name="updatename" />
            </div>
        </div>
    </form>
</div>
</body>
</html>
<script>
    {
        $(function () {
            addDivHtml=$("#addPermissionDiv").html();
            updateDivHtml=$("#updatePermissionDiv").html();
            selectPermission();
        })
    }
    {
        var ztreeObj;

        function selectPermission() {
            jqueryAjax("/CategoryController/selectArea.do",
                null,
                function (result) {
                    console.log( jqueryId("aouDiv form"),

                    );
                    if (result.code == 0) {
                        var listArea = result.data;

                        ztreeObj = $.fn.zTree.init($("#ztree"), setting, listArea);

                    }
                }
            )
        }
    }

    {
        var setting = {
            data: {
                simpleData: {
                    enable: true,
                    //idKey 指定的是id
                    //pIdKey 指定pid
                    idKey: "categoryId",
                    pIdKey: "categoryPid"
                },
                key: {
                    name: "categoryName"
                }
            },
            check: {
                enable: true,
            }
        }
    }


    function showAddPermissionDialog(){
        $("#addPermissionDiv").html(addDivHtml);

        var ztreeNodes = ztreeObj.getSelectedNodes();
        if(ztreeNodes.length>0){

            var fatherName = ztreeNodes[0].name;
            $("#addPermissionForm #parentName").val(fatherName);
            bootbox.confirm({
                message:$("#addPermissionDiv").children(),
                title:"新增地区",
                size:"lg",
                buttons:{
                    confirm:{
                        label:"确认",
                        className:"btn btn-success"
                    },
                    cancel:{
                        label:"取消",
                        className:"btn btn-danger"
                    }
                },
                callback:function(result){
                    if(result){
                        var data={};

                        jqueryAjax(
                            "/CategoryController/addArea.do",
                            {
                                categoryId:ztreeNodes[0].categoryId,
                                categoryName:jqueryName("name").val(),


                            },
                            function(result){
                                if(result.code == 0)
                                {
                                    selectPermission();
                                }
                            }
                        )
                    }
                }

            })
        }else{
            alert("请选择一个选项")
        }
    }

    function showUpdatePermissionDialog(){
        $("#updatePermissionDiv").html(updateDivHtml);
        var ztreeNodes=ztreeObj.getSelectedNodes();
        if(ztreeNodes.length>0){
            //获取选中的对象
            var ztreeNode = ztreeNodes[0];
            var areaId = ztreeNode.categoryId;

            jqueryAjax("/CategoryController/toUpdate.do",{areaId:areaId},
                function(result){
                    console.log(result);
                    if(result.code==0){

                        jqueryId("areaId").val(result.data.categoryId);
                        jqueryName("updatename").val(result.data.categoryName);

                    }
                }


            )
            bootBoxConfirm(jqueryId("updatePermissionDiv form"),"修改",
                function(type){ //确定才修改
                    if(type)
                    {
                        var data={};
                        /* data.araeName = jqueryName("name").val(),*/
                        data.categoryName = jqueryName("name").val(),
                            data.categoryId=jqueryId("areaId").val(),
                            //  debugger;
                            jqueryAjax(
                                "/CategoryController/updateArea.do",
                                {
                                    categoryName:jqueryName("updatename").val(),
                                    categoryId:jqueryId("areaId").val()
                                },
                                function(result){
                                    if(result.code == 0)
                                    {
                                        selectPermission();
                                    }
                                }
                            )
                        //  bootBoxAlert(result.msg);
                    }
                    $("#updatePermissionDiv").html(updateDivHtml);
                }
            )

        }else{
            alert("请选择要修改的选项");
        }
    }
    function showDeletePermissionDialog(){
        //获取所有选中的节点集合
        var ztreeNodes = ztreeObj.getSelectedNodes();
        if(ztreeNodes.length==1){

            bootbox.confirm({
                message:"确定要删除吗?",
                title:"删除",
                size:"lg",
                buttons:{
                    confirm:{
                        label:"确认",
                        className:"btn btn-success"
                    },
                    cancel:{
                        label:"取消",
                        className:"btn btn-danger"
                    }
                },
                callback:function(result){
                    if(result){
                        var data={};

                        var nodesArr = ztreeObj.transformToArray(ztreeNodes[0]);
                        var ids = [];
                        for(var i=0;i<nodesArr.length;i++){
                            ids.push(nodesArr[i].categoryId);
                        }
                        jqueryAjax("/CategoryController/deleteArea.do", {areaId: ids},
                            function (result) {
                                console.log(result);
                                if (result.code == 0) {

                                    ztreeObj.removeNode(ztreeNodes[0]);
                                    bootBoxAlert(result.msg);
                                }
                            }
                        )

                    }
                }

            })

        }else if(ztreeNodes.length==0){
            alert("请选择要删除的选项")
        }else{
            alert("请选择一个选项")
        }
    }

</script>
